/*
 * @Autor: dingxiaolin
 * @Date: 2021-06-02 13:12:42
 * @LastEditors: dingxiaolin
 * @LastEditTime: 2021-07-12 09:15:00
 */
import { App } from 'vue'
import { storage } from '@/utils/Storage'

/**
 * 判断是否拥有某种权限
 * @param {string | Array<string>} action
 * @param {boolean} Array
 */
export const hasPermission = (action: string | Array<string>, isArray = false) => {
    const permissions = storage.get('PERMISSIONS') || []
    const adminType = storage.get('ADMINTYPE')
    if (adminType == '1') return true;
    if (!isArray) {
        return permissions.some(item => item === action)
    } else {
        const data = action as Array<string>;
        return data.every(val => permissions.some(item => item === val));
    }
}
function hasPerm(value) {
    const permissions = storage.get('PERMISSIONS') || []
    const adminType = storage.get('ADMINTYPE')
    if (adminType == '1') {
        return true
    }
    return permissions.indexOf(value) > -1
}
// 暴露一个插件 API
const install = (app: App) => {
    app.config.globalProperties.$hasPerm = hasPerm
}
export default install
